﻿$(document).ready(function() {

	$.namespace("income", {
		inherited: true,

		InitialiseControl: function() {
			$.fn.income.LoadIncomeGrid();
		},

		LoadIncomeGrid: function() {

			var tabContent = $("#IncomesContainer").children(".TabContent");

			var proxy = new Ext.data.HttpProxy({
				url: 'Data/Incomes.aspx',
				listeners: {
					'write': function(proxy, action, data, response, records, options) {
						if (action == 'create') {
							records.store.loadData(response);
						}
					}
				}
			});

			var reader = new Ext.data.JsonReader({
				root: 'Data',
				totalProperty: 'Count',
				idProperty: 'IncomeId',
				fields: [
					{ name: 'IncomeId', type: 'int' },
					{ name: 'Description', type: 'string', allowBlank: false },
					{ name: 'Amount', type: 'float', allowBlank: false },
					{ name: 'IsActive', type: 'boolean' },
					{ name: 'Timestamp', type: 'date', dateFormat: 'M$' },
					{ name: 'Version', type: 'auto' }
				]
			});

			var writer = new Ext.data.JsonWriter({
				encode: true,   // <-- don't return encoded JSON -- causes Ext.Ajax#request to send data using jsonData config rather than HTTP params
				writeAllFields: true
			});

			var store = new Ext.data.Store({
				id: 'incomes',
				restful: false,     // <-- This Store is RESTful
				proxy: proxy,
				reader: reader,
				writer: writer,    // <-- plug a DataWriter into the store just as you would a Reader
				sortInfo: {
					field: 'Description',
					direction: 'ASC'
				},
				listeners: {
					'update': function(store, record, operation) {
						//alert('store update event is in action');
						store.commitChanges();
					},
					'remove': function(store, record, index) {
						//alert('store remove event is in action');
						store.commitChanges();
					},
					'save': function(store, batchNumber, data) {
						alert('store save event is in action');
					},
					'write': function(store, action, result, transaction, rs) {
						alert('store write event is in action');
					}
				}
			});

			// plugins
			var editor = new Ext.ux.grid.RowEditor({
				saveText: 'Update'
			});
			Ext.ux.grid.GroupSummary.Calculations['totalIncome'] = function(v, record, field) {
				if (true) { }
				var val = v + (record.data.estimate * record.data.rate);
				return val;
			};
			var summary = new Ext.ux.grid.GroupSummary();

			//column model
			var colModel = new Ext.grid.ColumnModel({
				defaults: {
					sortable: true
				},
				columns: [
				//{ header: 'Id', dataIndex: 'IncomeId' },
					{header: 'Description', dataIndex: 'Description', editor: new Ext.form.TextField({}) },//, summaryRenderer: function(v, params, data) { return ((v === 0 || v > 1) ? '(' + v + ' Incomes)' : '(1 Income)'); } },
					{ header: 'Amount', dataIndex: 'Amount', xtype: 'numbercolumn', align: 'right', format: '$0,0.00', editor: new Ext.form.NumberField({}) },//, summaryType: 'sum' },
					{ header: 'Active', dataIndex: 'IsActive', xtype: 'booleancolumn', align: 'center', trueText: 'Yes', falseText: 'No', editor: new Ext.form.Checkbox({}) }
				]
			});


			var grid = new Ext.grid.GridPanel({
				//renderTo: 'DataGrid',
				iconCls: 'icon-grid',
				frame: true,
				title: 'Incomes',
				loadMask: true,
				autoScroll: true,
				height: 300,
				store: store,
				plugins: [editor],//, summary],
				colModel: colModel,
				tbar: [{
					text: 'Add',
					iconCls: 'silk-add',
					handler: function(btn, ev) {
						var u = new grid.store.recordType({
							Description: '',
							Amount: '',
							IsActive: true
						});
						editor.stopEditing();
						grid.store.insert(0, u);
						editor.startEditing(0);
					}
				}, '-', '->', '-', {
					text: 'refresh',
					iconCls: 'silk-table-refresh',
					handler: function onDelete() {
						grid.store.load();
					}
				}],
				viewConfig: {
					forceFit: true
				}
			});

			grid.render(tabContent[0]);

			store.load();
		}
	});

	//		$.fn.income.InitialiseControl();
});